package io.xiyou.order.message;


import com.alibaba.fastjson.JSON;
import io.xiyou.order.api.IOrderService;
import io.xiyou.order.api.bo.AddOrderBO;
import io.xiyou.order.api.dto.OrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.CountDownLatch;

/**
 * @ClassName：ReceiverRedisMessage
 * @Author：西柚
 * @Date：2021/10/23 4:38 下午
 * @Versiion：1.0
 */
@Slf4j
@Component
public class ReceiverRedisMessage {

    @Autowired
    private IOrderService orderService;

    private CountDownLatch latch;

    @Autowired
    public ReceiverRedisMessage(CountDownLatch latch) {
        this.latch = latch;
    }

    /**
     * 队列消息接收方法
     *
     * @param jsonMsg
     */
    public void receiveMessage(String jsonMsg) {
        log.info("[开始消费redis消息队列订单数据...]");
        try {
            log.info("请求参数：{}", jsonMsg);

            // \会被转译，先解析
            Object parse1 = JSON.parse(jsonMsg);

            AddOrderBO addOrderBO = JSON.parseObject(parse1.toString(), AddOrderBO.class);

            OrderDTO order = orderService.createOrder(addOrderBO);

            log.info("创建订单结果：{}", JSON.toJSONString(order));

            log.info("[消费redis消息队列订单数据成功.]");
        } catch (Exception e) {
            log.error("[消费redis消息队列订单数据失败，失败信息:{}]", e);
        }
        latch.countDown();
    }

}
